/*
This file produces plots for the paper that pertain to the model
*/

clear
*local user sgilbukh
local user sophiagilbukh
cd /Users/sophiagilbukh/Library/CloudStorage/Dropbox/Research/GGP_Brokers/Submissions/rfs_acceptance/code_replication/theory
global output_dir output
set more off
set scheme sonia_new


*************************************************************
**** Load all of the model results data *********************
*************************************************************

* specify which ones we want
global model Baseline Baseline_flexc Baseline_flexcBF Com_EffAvg Com_BFEffAvg


forvalues i=85/105 {
	global model $model EntryCost`i' EntryCost`i'BF 
}

di "$model"

foreach i in $model {
    di "`i'"
*[State(1,:)' State(3,:)' (Ne+Ne_add)' (Nb+Nb_add)' Sale_p EProfit' policy dist' lambd(State(3,:))' Vs(State(3,:))' Buy_p];
	import delimited using results/`i'_data.csv, clear
	rename v1 experience
	rename v2 state
	rename v3 listings`i'
	rename v4 buyers`i'
	rename v5 sellp`i'
	rename v6 buyp_st`i'	
	rename v7 profit`i'
	rename v8 exit_rate`i'
	rename v9 dist`i'
	rename v10 entry`i'
	rename v11 sellV`i'
	rename v12 mu
	rename v13 sigma
	rename v14 c_e`i'
	rename v15 delt`i'
	rename v16 b_model // listing accumulation
	rename v17 failed`i' // listing accumulation
	cap rename v18 buyV`i'
	cap rename v19 commission`i'
	gen num_bs_`i' = listings`i' +buyers`i'
	replace experience = experience-1
	gen learning`i' = listings`i' + buyers`i'- experience
	
	gen seller_dist`i' = dist`i'*listings`i'
	sort state experience
	by state: egen num_sellers_t`i' = total(seller_dist`i')
	by state: egen num_agents`i' = total(dist`i')
	replace entry`i' = entry`i'/num_agents`i'
	gen  density`i' = dist`i'/num_agents`i'
	
	gen q = 0
	replace q = 0.2209 if state==1
	replace q = 0.052 if state==2
	replace q = 0.065 if state==3
	replace q = 0.075 if state==4
	replace q = 0.1864 if state==5
	replace q = 0.0621 if state==6
	replace q = 0.0424 if state==7
	replace q = 0.0847 if state==8
	replace q = 0.2119 if state==9
	egen wt_agents`i' = total(dist`i'*q)

	
	preserve
	
	cap merge 1:1 experience state using `temp'
	cap drop _merge
			
	tempfile temp
	save `temp'
	
	restore
	
	gen list_market`i' = listings`i'*dist`i'
	gen sold_market`i' = sellp`i'*listings`i'*dist`i'
	
	collapse (sum) list_state`i' = list_market`i' sold_state`i' = sold_market`i' (mean) buyV`i' sellV`i' q buyp_st`i', by(state)
	
	gen sellp_st`i' = sold_state`i'/list_state`i'
    gen buyers_state`i' = sold_state`i'/buyp_st`i'
	
	egen sellp_wt`i' = total(sellp_st`i'*q)
	egen buyp_wt`i' = total(buyp_st`i'*q)
	
	egen sellV_wt`i' = total(sellV`i'*q)
	egen buyV_wt`i' = total(buyV`i'*q)
	
	merge 1:m state using `temp'
	cap drop _merge
    tempfile temp
	save `temp'
	
	gen liq_improvement`i' = (sellp_wt`i' - sellp_wtBaseline)/sellp_wtBaseline
	gen buy_liq_imp`i' = (buyp_wt`i' - buyp_wtBaseline)/buyp_wtBaseline
	tempfile temp
	save `temp'
	
	/*
	* general average buy probabilities by state and overall
	by state: egen total_buyers`i' = total(sellp`i'*listings`i'*dist`i'/buyp_st`i')
	by state: gen st_buyp_st`i' = total_sold_state`i'/total_buyers`i'
	egen wtall_buyp_st`i' = wtmean(gen st_buyp_st`i'), weight(q)
	
	* general average seller value overall
	egen wtall_sellV`i' = wtmean(gen st_sellV`i'), weight(q)
	
	* general average buyer value overall
	egen wtall_buyV`i' = wtmean(gen st_buyV`i'), weight(q)
	
		gen liq_improvement`i' = (wtsold`i' - wtsoldBaseline)/wtsoldBaseline
	gen stliq_imprt`i' = (total_sold_state`i' - total_sold_stateBaseline)/total_sold_stateBaseline

*/
}

sort state experience
replace experience = experience + 1
merge 1:1 state experience using data_moments
tab _merge
keep if _merge==3
drop _merge

replace experience = experience - 1

gen bust=1 if state==1 | state==4 | state==7
gen medium=1 if state==2 | state==5 | state==8
gen boom=1 if state==3 | state==6 | state==9

* Define the models manually see which ones lead to liq improvements that are the same as flexcommission
foreach i in EntryCost EntryCostBF {
	if "`i'"== "EntryCost" local j EntryCost104
	if "`i'"== "EntryCostBF" local j EntryCost88BF 
	rename listings`j' listings`i'
	rename buyers`j' buyers`i'
	rename sellp`j' sellp`i'
	rename buyp_st`j'	 buyp_st`i'
	rename profit`j' profit`i'
	rename exit_rate`j' exit_rate`i'
	rename dist`j' dist`i'
	rename density`j' density`i'
	rename entry`j' entry`i'
	rename sellV`j' sellV`i'
	rename buyV`j' buyV`i'
	rename sellV_wt`j' sellV_wt`i'
	rename buyV_wt`j' buyV_wt`i'
	rename delt`j' delt`i' 
	rename learning`j' learning`i'
	rename sellp_st`j' sellp_st`i'
	rename sellp_wt`j' sellp_wt`i'
	rename buyp_wt`j' buyp_wt`i'
}
global ceEC "124,000"
global ceECBF "108,000"

/*
*** Plot Commissions For the Flexible Commission Exercise ***
replace seller_commission = seller_commission*100
twoway (line seller_commission experience if state==1) (line seller_commission experience if state==2)  (line seller_commission experience if state==3), legend(label(1 "Bust") label(2 "Medium") label(3 "Boom")) ytitle("Seller Agent Commission (%)") xtitle("Experience") 
graph export $output_dir/flexible_commission.pdf
*/

replace commissionBaseline_flexcBF = commissionBaseline_flexcBF*100
twoway (line commissionBaseline_flexcBF experience if state==1) (line commissionBaseline_flexcBF experience if state==2)  (line commissionBaseline_flexcBF experience if state==3), legend(label(1 "Bust") label(2 "Medium") label(3 "Boom")) ytitle("Seller Agent Commission (%)") xtitle("Experience")
graph export $output_dir/flexible_commissionBF.pdf, replace

replace commissionBaseline_flexc = commissionBaseline_flexc*100
twoway (line commissionBaseline_flexc experience if state==1) (line commissionBaseline_flexc experience if state==2)  (line commissionBaseline_flexc experience if state==3), legend(label(1 "Bust") label(2 "Medium") label(3 "Boom")) ytitle("Seller Agent Commission (%)") xtitle("Experience") 
graph export $output_dir/flexible_commission.pdf, replace


******** Sale Probability (only varies by z) ****************

foreach period in bust medium boom {
        if "`period'"=="bust" local lb .4 
		if "`period'"=="bust" local ub .55 
		if "`period'"=="medium" local lb .45 
		if "`period'"=="medium" local ub .6 
		if "`period'"=="boom" local lb .55 
		if "`period'"=="boom" local ub .7 
			
		 sort experience
		 twoway (line sellpBaseline experience if `period'==1, lw(medthick)) (scatter sale_prob_data experience if `period'==1), ytitle("Probability of Sale") xtitle("Experience") ///
				legend(label(1 "Baseline") label(2 "Data")) yl(`lb'(0.05)`ub')
			graph export $output_dir/model_sellpBaseline`period'poisson.pdf, replace
			
		twoway (line sellpBaseline experience if `period'==1, lw(medthick)) (scatter sale_prob_data experience if `period'==1), ytitle("Probability of Sale") xtitle("Experience") ///
				legend(off) yl(`lb'(0.05)`ub')
			graph export $output_dir/model_sellpBaseline`period'poisson_noleg.pdf, replace

}
******************************************/

****************** Distribution *************
global all_samples EntryCost EntryCostBF Baseline_flexc Baseline_flexcBF Com_EffAvg Com_BFEffAvg

foreach state of num 10(1)10 {
    if `state'==10 { // average across all aggregate states
	   preserve 
	   collapse entry* list* buyer* sale_p* profit* exit* dist* dens* num_ag* [aw=q], by(experience)
	   gen state=10
	}
	
	foreach i in $all_samples  {	
		
			
	if "`i'"=="EntryCost" local legd "Entry Cost: $$ceEC" 
	if "`i'"=="EntryCostBF" local legd "Entry Cost: $$ceECBF" 
		if "`i'"=="Baseline_flexc" local legd "Flexible Commission" 
		if "`i'"=="Baseline_flexcBF" local legd "Flexible Commission"
		if "`i'"=="Com_EffAvg" local legd "Fixed Commission (1.53\%)" 
		if "`i'"=="Com_BFEffAvg" local legd "Fixed Commission (1.61\%)"
	if "`i'"=="_data" local legd "Data" 
	
		twoway (bar densityBaseline experience if state==`state', barw(1) lcolor(white) fcolor(navy)) ///
	       (bar density`i' experience if state==`state', barw(1) lcolor(black) lwidth(thin) fcolor(none)), ///
		   xtitle("Experience") ytitle("Density") legend(lab(1 "Baseline") lab(2 "`legd'")) // (line y x, yaxis(2))
		graph export $output_dir/exp_distBaseline`i'`state'poisson.pdf, replace		
	}
	
	cap restore
	if `state'==10 { // average across all aggregate states
	   preserve 
	   collapse entry* list* buyer* sale_p* profit* exit* dist* dens* num_ag* [aw=q] if ~missing(dist_data), by(experience)
	   gen state=10
	}
	foreach i in _data {	
		
	if "`i'"=="_data" local legd "Data" 
	
		twoway (bar densityBaseline experience if state==`state', barw(1) lcolor(white) fcolor(navy)) ///
	       (bar density`i' experience if state==`state', barw(1) lcolor(black) lwidth(thin) fcolor(none)), ///
		   xtitle("Experience") ytitle("Density") legend(lab(1 "Baseline") lab(2 "`legd'")) // (line y x, yaxis(2))
		graph export $output_dir/exp_distBaseline`i'`state'poisson.pdf, replace		
	}
		
}

restore
**********************************/

******* Exit Rates ***************
set more off
foreach state of num 10(1)10 {
    if `state'==10 { //  average across all aggregate states
	   preserve 
	   collapse entry* exit* [aw=q], by(experience) 
	   gen state=10
	}
	
		local yl "Exit Rate" 
		sum entryBaseline if state==`state'
		local mean_c = string(`r(mean)' , "%9.2f")
		local entry_base "Entry Rate (Baseline) = `mean_c'"
		di "`entry_base'"

		foreach i in $all_samples {
	if "`i'"=="EntryCost" local legd "Entry Cost: $$ceEC" 
	if "`i'"=="EntryCostBF" local legd "Entry Cost: $$ceECBF" 
		if "`i'"=="Baseline_flexc" local legd "Flexible Commission" 
		if "`i'"=="Baseline_flexcBF" local legd "Flexible Commission"
		if "`i'"=="Com_EffAvg" local legd "Fixed Commission (1.53\%)" 
		if "`i'"=="Com_BFEffAvg" local legd "Fixed Commission (1.61\%)"
		if "`i'"=="_data" local legd "Data" 
	
	
		    if ("`i'"=="_data" & (`state'==1 | `state'==2 | `state'==4 | `state'==6 | `state'==8 | `state'==9 | `state'==10 )) | ("`i'" ~="_data") {
				disp "State","`state'"
				sum entry`i' if state==`state'
				local mean_c = string(`r(mean)' , "%9.2f")
				local entry_base_comp "Entry Rate (Counterf.) = `mean_c'"
				if "`i'"=="_data" local entry_base_comp "Entry Rate (Data) = `mean_c'"
				
			
				twoway (connected exit_rateBaseline experience if state==`state') (connected exit_rate`i' experience if state==`state'), yl(0(0.1)0.55) ytitle(`yl') xtitle("Experience") ///
				legend(label(1 "Baseline") label(2 "`legd'")) text(.3 30 "`entry_base'")  text(.33 30 "`entry_base_comp'") 
				graph export $output_dir/model_exit_rate_`i'`state'poisson.pdf, replace
				
		   }
		}
		
	 cap restore	
      if `state'==10 { //  average across all aggregate states
	   preserve 
	   collapse entry* exit* [aw=q]  if ~missing(exit_rate_data), by(experience) 
	   gen state=10
	}
	
		local yl "Exit Rate" 
		sum entryBaseline if state==`state'
		local mean_c = string(`r(mean)' , "%9.2f")
		local entry_base "Entry Rate (Baseline) = `mean_c'"
		di "`entry_base'"

		foreach i in  _data {
			
		if "`i'"=="_data" local legd "Data" 
	
	
		    if ("`i'"=="_data" & (`state'==1 | `state'==2 | `state'==4 | `state'==6 | `state'==8 | `state'==9 | `state'==10 )) | ("`i'" ~="_data") {
				disp "State","`state'"
				sum entry`i' if state==`state'
				local mean_c = string(`r(mean)' , "%9.2f")
				local entry_base_comp "Entry Rate (Counterf.) = `mean_c'"
				if "`i'"=="_data" local entry_base_comp "Entry Rate (Data) = `mean_c'"
				
			
				twoway (connected exit_rateBaseline experience if state==`state') (connected exit_rate`i' experience if state==`state'), yl(0(0.1)0.55) ytitle(`yl') xtitle("Experience") ///
				legend(label(1 "Baseline") label(2 "`legd'")) text(.3 30 "`entry_base'")  text(.33 30 "`entry_base_comp'") 
				graph export $output_dir/model_exit_rate_`i'`state'poisson.pdf, replace
				
		   }
		}
	} 
cap restore
********************************/

***************** Plot Learning  ************************************
set more off
foreach state of num 10(1)10 {
    if `state'==10 { // average across all aggregate states
	   preserve 
	   collapse learn* [aweight=q], by(experience)
	   gen state=10
	}		
	foreach i in $all_samples {
		
	if "`i'"=="EntryCost" local legd "Entry Cost: $$ceEC" 
	if "`i'"=="EntryCostBF" local legd "Entry Cost: $$ceECBF" 
		if "`i'"=="Baseline_flexc" local legd "Flexible Commission" 
		if "`i'"=="Baseline_flexcBF" local legd "Flexible Commission"
		if "`i'"=="Com_EffAvg" local legd "Fixed Commission (1.53\%)" 
		if "`i'"=="Com_BFEffAvg" local legd "Fixed Commission (1.61\%)"
		if "`i'"=="_data" local legd "Data" 
			
			twoway (bar learningBaseline experience if state==`state' & experience<30, barw(1) lcolor(white) fcolor(navy)) ///
			   (bar learning`i' experience if state==`state' & experience<30, barw(1) lcolor(black) lwidth(thin) fcolor(none)), ///
			   xtitle("Experience") ytitle("{&Delta} Experience") legend(lab(1 "Baseline") lab(2 `legd')) // (line y x, yaxis(2))
			graph export $output_dir/learningBaseline`i'`state'poisson.pdf, replace		
			more
		}
}
cap restore
			
foreach state of num 10(1)10 {	
      if `state'==10 { // average across all aggregate states
	   preserve 
	   collapse learn* [aweight=q] if ~missing(learning_data), by(experience)
	   gen state=10
	}		
	foreach i in _data{
		
		if "`i'"=="_data" local legd "Data" 
			
			twoway (bar learningBaseline experience if state==`state' & experience<30, barw(1) lcolor(white) fcolor(navy)) ///
			   (bar learning`i' experience if state==`state' & experience<30, barw(1) lcolor(black) lwidth(thin) fcolor(none)), ///
			   xtitle("Experience") ytitle("{&Delta} Experience") legend(lab(1 "Baseline") lab(2 `legd')) // (line y x, yaxis(2))
			graph export $output_dir/learningBaseline`i'`state'poisson.pdf, replace		
			more
		}
		
	} 
cap restore
****************************************************************/

*************** Model fit summary ***************
**** Adjusted some extra line breaks by hand
estimates clear
preserve

** Exit and Entry by State
foreach exp_point in 0 10 40 {
	gen exit_rateBaseline`exp_point' = exit_rateBaseline if experience==`exp_point'
	gen exit_rate_data`exp_point'  = exit_rate_data if experience==`exp_point'
}
estpost tabstat exit_rateBaseline0 exit_rate_data0 exit_rateBaseline10 exit_rate_data10 exit_rateBaseline40 exit_rate_data40  entryBaseline entry_data, statistics(mean) by(state) notot

esttab using output/model_fit.tex, replace  ///
    cells("exit_rateBaseline0(fmt(%12.2fc)) exit_rate_data0(fmt(%12.2fc)) exit_rateBaseline10(fmt(%12.2fc)) exit_rate_data10(fmt(%12.2fc)) exit_rateBaseline40(fmt(%12.2fc)) exit_rate_data40(fmt(%12.2fc)) entryBaseline(fmt(%12.2fc)) entry_data(fmt(%12.2fc))") /// 
    noobs nonumber ///
	fragment ///
	coeflabels(1 "Bust  Bust  " 2 "Bust  Medium  " 3 "Bust  Boom  " 4 "Medium  Bust  " 5 "Medium  Medium  " 6 "Medium  Boom  " 7 "Boom  Bust  " 8 "Boom  Medium  " 9 "Boom  Boom  ") ///
	collabels("Baseline"  "Data" "Baseline"  "Data" "Baseline"  "Data" "Baseline"  "Data") ///
	prehead("\textit{Panel A:} & \multicolumn{6}{c}{Exit Rates} & \multicolumn{2}{c}{Entry Rates} \\ \cmidrule(l){2-7} \cmidrule(l){8-9} \\ & \multicolumn{2}{c}{Experience 0 } & \multicolumn{2}{c}{Experience 10 } & \multicolumn{2}{c}{Experience 40 } & & \\ \cmidrule(l){2-3} \cmidrule(l){4-5} \cmidrule(l){6-7}")  booktabs
	 
** Distribution: average experience in each quartile of distirbution
* compute percentiles
sort state experience
by state: gen cumdensBaseline = sum(densityBaseline) 
by state: gen cumdensdata = sum(density_data) 
foreach prc in 25 50 75 95{
	di "`prc'"
    gen exp`prc'Baseline = .
	gen exp`prc'data = .
    levelsof state 
    foreach x in `r(levels)' {
	    di "`x'"
		sum experience if 100*cumdensBaseline>`prc' & state==`x'
		replace exp`prc'Baseline = `r(min)' if state==`x'
		
		sum experience if 100*cumdensdata>`prc' & state==`x'
		cap replace exp`prc'data = `r(min)' if state==`x'
	}
}
estpost tabstat exp25Baseline exp25data exp50Baseline exp50data exp75Baseline exp75data exp95Baseline exp95data, statistics(mean) by(state) notot

esttab using output/model_fit.tex, append  ///
    cells("exp25Baseline(fmt(%12.0fc)) exp25data(fmt(%12.0fc)) exp50Baseline(fmt(%12.0fc)) exp50data(fmt(%12.0fc)) exp75Baseline(fmt(%12.0fc)) exp75data(fmt(%12.0fc)) exp95Baseline(fmt(%12.0fc)) exp95data(fmt(%12.0fc))") /// 
    noobs nonumber ///
	fragment ///
	coeflabels(1 "Bust  Bust  " 2 "Bust  Medium  " 3 "Bust  Boom  " 4 "Medium  Bust  " 5 "Medium  Medium  " 6 "Medium  Boom  " 7 "Boom  Bust  " 8 "Boom  Medium  " 9 "Boom  Boom  ") ///
	collabels("Baseline"  "Data" "Baseline"  "Data" "Baseline"  "Data"  "Baseline"  "Data" ) booktabs ///
	prehead("\midrule \\ \textit{Panel B:} & multicolumn{8}{c}{Distribution} \\ \cmidrule(l){2-9} 	& \multicolumn{2}{c}{25th Percentile } & \multicolumn{2}{c}{50th Percentile } & \multicolumn{2}{c}{75th Percentile} & \multicolumn{2}{c}{95th Percentile} \\ \cmidrule(l){2-3} \cmidrule(l){4-5} \cmidrule(l){6-7}  \cmidrule(l){8-9}") 

** Learning: learning 	
	foreach exp_point in 0 5 10 40 {
	gen learningBaseline`exp_point' = learningBaseline if experience==`exp_point'
	gen learning_data`exp_point'  = learning_data if experience==`exp_point'
}
estpost tabstat learningBaseline0 learning_data0 learningBaseline5 learning_data5 learningBaseline10 learning_data10 learningBaseline40 learning_data40, statistics(mean) by(state) notot

esttab using output/model_fit.tex, append  ///
    cells("learningBaseline0(fmt(%12.1fc)) learning_data0(fmt(%12.1fc)) learningBaseline5(fmt(%12.1fc)) learning_data5(fmt(%12.1fc)) learningBaseline10(fmt(%12.1fc)) learning_data10(fmt(%12.1fc)) learningBaseline40(fmt(%12.1fc)) learning_data40(fmt(%12.1fc))") /// 
    noobs nonumber ///
	fragment ///
	coeflabels(1 "Bust  Bust  " 2 "Bust  Medium  " 3 "Bust  Boom  " 4 "Medium  Bust  " 5 "Medium  Medium  " 6 "Medium  Boom  " 7 "Boom  Bust  " 8 "Boom  Medium  " 9 "Boom  Boom  ") ///
	collabels("Baseline"  "Data" "Baseline"  "Data" "Baseline"  "Data" "Baseline"  "Data" "Baseline"  "Data" ) booktabs ///
	prehead("\midrule \\ \textit{Panel C:} & \multicolumn{8}{c}{Learning} \\ \cmidrule(l){2-9} 	& \multicolumn{2}{c}{Experience 0} & \multicolumn{2}{c}{Experience 5 } & \multicolumn{2}{c}{Experience 10} & \multicolumn{2}{c}{Experience 40} \\ 	 \cmidrule(l){2-3} \cmidrule(l){4-5} \cmidrule(l){6-7}  \cmidrule(l){8-9}" ) 

restore

************************************/




************************* Counterfactuals Table Main *******************

estimates clear
gen aggr_state = 1 if bust==1
replace aggr_state = 2 if medium==1
replace aggr_state=3 if boom==1

************************* Counterfactuals Table Main (3 panels) *******************
	*** Baseline 
estpost tabstat sellp_stBaseline buyp_stBaseline sellVBaseline buyVBaseline, statistics(mean) by(aggr_state) notot
esttab using output//counterfactual_results_all.tex, replace  ///
    cells("sellp_stBaseline(fmt(%12.3fc)) buyp_stBaseline(fmt(%12.3fc)) sellVBaseline(fmt(%12.0fc)) buyVBaseline(fmt(%12.0fc))") ///
    noobs nonumber ///
	fragment ///
	prehead("\begin{tabular}{l*{1}{cccc}} \toprule") ///
	posthead("\hline \multicolumn{5}{c}{Panel A: Baseline} \\ \cmidrule(l){2-5}&            &            &            \\" ) ///
	coeflabels(1 "Bust" 2 "Medium" 3 "Boom") ///
	collabels("Sale Prob" "Buy Prob" "Seller Value" "Buyer Value") ///
	mlabels("",prefix(\multicolumn{4}{c}{Baseline} \\ \cmidrule(l){2-5})) booktabs

estimates clear	
estpost tabstat sellp_wtBaseline buyp_wtBaseline sellV_wtBaseline buyV_wtBaseline, statistics(mean)
foreach i in sellp_wtBaseline buyp_wtBaseline sellV_wtBaseline buyV_wtBaseline {
    matrix `i'_new  = e(`i')
    matrix colnames `i'_new = Total 
    estadd matrix  `i'_new 
}
esttab using output//counterfactual_results_all.tex,  ///
    cells("sellp_wtBaseline_new(fmt(%12.3fc)) buyp_wtBaseline_new(fmt(%12.3fc)) sellV_wtBaseline_new(fmt(%12.0fc)) buyV_wtBaseline_new(fmt(%12.0fc))") ///
	fragment ///
	append /// 
	prefoot("\bottomrule") ///
    mlabels(,none) collabels(,none) ///
    noobs nonumber noline nomtitles ///
	booktabs
	
	*** Efficiency Benchmark
	estpost tabstat sellp_stBaseline_flexc buyp_stBaseline_flexc sellVBaseline_flexc buyVBaseline_flexc, statistics(mean) by(aggr_state) notot
esttab using output//counterfactual_results_all.tex,  ///
    cells("sellp_stBaseline_flexc(fmt(%12.3fc)) buyp_stBaseline_flexc(fmt(%12.3fc)) sellVBaseline_flexc(fmt(%12.0fc)) buyVBaseline_flexc(fmt(%12.0fc))") ///
	posthead("\hline \multicolumn{5}{c}{Panel B: Flexible Commission Benchmark} \\ \cmidrule(l){2-5}&            &            &            \\" ) ///
    noobs nonumber ///
	fragment ///
	append /// 
	coeflabels(1 "Bust" 2 "Medium" 3 "Boom") ///
	collabels(,none) ///
	mlabels("",none) booktabs
	
	estpost tabstat sellp_wtBaseline_flexc buyp_wtBaseline_flexc sellV_wtBaseline_flexc buyV_wtBaseline_flexc, statistics(mean) 
foreach i in sellp_wtBaseline_flexc buyp_wtBaseline_flexc sellV_wtBaseline_flexc buyV_wtBaseline_flexc {
    matrix `i'n  = e(`i')
    matrix colnames `i'n = Total 
    estadd matrix  `i'n 
}
esttab using output//counterfactual_results_all.tex,  ///
    cells("sellp_wtBaseline_flexcn(fmt(%12.3fc)) buyp_wtBaseline_flexcn(fmt(%12.3fc)) sellV_wtBaseline_flexcn(fmt(%12.0fc)) buyV_wtBaseline_flexcn(fmt(%12.0fc))") ///
	fragment ///
	append /// 
	prefoot("\bottomrule") ///
    mlabels(,none) collabels(,none) ///
    noobs nonumber noline nomtitles ///
	booktabs
	
*** Fixed Commission to match sale average committion of the Efficiency Benchmark
	estpost tabstat sellp_stCom_EffAvg buyp_stCom_EffAvg sellVCom_EffAvg buyVCom_EffAvg, statistics(mean) by(aggr_state) notot
esttab using output//counterfactual_results_all.tex,  ///
    cells("sellp_stCom_EffAvg(fmt(%12.3fc)) buyp_stCom_EffAvg(fmt(%12.3fc)) sellVCom_EffAvg(fmt(%12.0fc)) buyVCom_EffAvg(fmt(%12.0fc))") ///
	posthead("\hline \multicolumn{5}{c}{Panel C: Fixed Commission 1.53\%} \\ \cmidrule(l){2-5}&            &            &            \\" ) ///
    noobs nonumber ///
	fragment ///
	append /// 
	coeflabels(1 "Bust" 2 "Medium" 3 "Boom") ///
	collabels(,none) ///
	mlabels("",none) booktabs
	
	estpost tabstat sellp_wtCom_EffAvg buyp_wtCom_EffAvg sellV_wtCom_EffAvg buyV_wtCom_EffAvg, statistics(mean) 
foreach i in sellp_wtCom_EffAvg buyp_wtCom_EffAvg sellV_wtCom_EffAvg buyV_wtCom_EffAvg {
    matrix `i'n  = e(`i')
    matrix colnames `i'n = Total 
    estadd matrix  `i'n 
}
esttab using output//counterfactual_results_all.tex,  ///
    cells("sellp_wtCom_EffAvg(fmt(%12.3fc)) buyp_wtCom_EffAvg(fmt(%12.3fc)) sellV_wtCom_EffAvg(fmt(%12.0fc)) buyV_wtCom_EffAvg(fmt(%12.0fc))") ///
	fragment ///
	append /// 
	prefoot("\bottomrule") ///
    mlabels(,none) collabels(,none) ///
    noobs nonumber noline nomtitles ///
	booktabs

	
*** Entry Cost  - not fixing buyers
	estpost tabstat sellp_stEntryCost buyp_stEntryCost sellVEntryCost buyVEntryCost, statistics(mean) by(aggr_state) notot
esttab using output//counterfactual_results_all.tex,  ///
    cells("sellp_stEntryCost(fmt(%12.3fc)) buyp_stEntryCost(fmt(%12.3fc)) sellVEntryCost(fmt(%12.0fc)) buyVEntryCost(fmt(%12.0fc))") ///
	posthead("\hline \multicolumn{5}{c}{Panel C: Increased Entry Cost} \\ \cmidrule(l){2-5}&            &            &            \\" ) ///
    noobs nonumber ///
	fragment ///
	append /// 
	coeflabels(1 "Bust" 2 "Medium" 3 "Boom") ///
	collabels(,none) ///
	mlabels("",none) booktabs
	
	
estpost tabstat sellp_wtEntryCost buyp_wtEntryCost sellV_wtEntryCost buyV_wtEntryCost, statistics(mean) 
	foreach i in sellp_wtEntryCost buyp_wtEntryCost sellV_wtEntryCost buyV_wtEntryCost {
    matrix `i'n  = e(`i')
    matrix colnames `i'n = Total 
    estadd matrix  `i'n 
}
esttab using output//counterfactual_results_all.tex,  ///
    cells("sellp_wtEntryCostn(fmt(%12.3fc)) buyp_wtEntryCostn(fmt(%12.3fc)) sellV_wtEntryCostn(fmt(%12.0fc)) buyV_wtEntryCostn(fmt(%12.0fc))") ///
 	fragment ///
	append /// 
	prefoot("\bottomrule") ///
    postfoot("\end{tabular}") ///
    mlabels(,none) collabels(,none) ///
    noobs nonumber noline nomtitles ///
	booktabs
	
************************* Counterfactuals Table Buyer Fix  (3 panels) *******************

	*** Baseline 
estpost tabstat sellp_stBaseline buyp_stBaseline sellVBaseline buyVBaseline, statistics(mean) by(aggr_state) notot
esttab using output//counterfactual_results_allBF.tex, replace  ///
    cells("sellp_stBaseline(fmt(%12.3fc)) buyp_stBaseline(fmt(%12.3fc)) sellVBaseline(fmt(%12.0fc)) buyVBaseline(fmt(%12.0fc))") ///
    noobs nonumber ///
	fragment ///
	prehead("\begin{tabular}{l*{1}{cccc}} \toprule") ///
	posthead("\hline \multicolumn{5}{c}{Panel A: Baseline} \\ \cmidrule(l){2-5}&            &            &            \\" ) ///
	coeflabels(1 "Bust" 2 "Medium" 3 "Boom") ///
	collabels("Sale Prob" "Buy Prob" "Seller Value" "Buyer Value") ///
	mlabels("",prefix(\multicolumn{4}{c}{Baseline} \\ \cmidrule(l){2-5})) booktabs

estimates clear	
estpost tabstat sellp_wtBaseline buyp_wtBaseline sellV_wtBaseline buyV_wtBaseline, statistics(mean)
foreach i in sellp_wtBaseline buyp_wtBaseline sellV_wtBaseline buyV_wtBaseline {
    matrix `i'_new  = e(`i')
    matrix colnames `i'_new = Total 
    estadd matrix  `i'_new 
}
esttab using output//counterfactual_results_allBF.tex,  ///
    cells("sellp_wtBaseline_new(fmt(%12.3fc)) buyp_wtBaseline_new(fmt(%12.3fc)) sellV_wtBaseline_new(fmt(%12.0fc)) buyV_wtBaseline_new(fmt(%12.0fc))") ///
	fragment ///
	append /// 
	prefoot("\bottomrule") ///
    mlabels(,none) collabels(,none) ///
    noobs nonumber noline nomtitles ///
	booktabs
	
	*** Efficiency Benchmark
	estpost tabstat sellp_stBaseline_flexcBF buyp_stBaseline_flexcBF sellVBaseline_flexcBF buyVBaseline_flexcBF, statistics(mean) by(aggr_state) notot
esttab using output//counterfactual_results_allBF.tex,  ///
    cells("sellp_stBaseline_flexcBF(fmt(%12.3fc)) buyp_stBaseline_flexcBF(fmt(%12.3fc)) sellVBaseline_flexcBF(fmt(%12.0fc)) buyVBaseline_flexcBF(fmt(%12.0fc))") ///
	posthead("\hline \multicolumn{5}{c}{Panel B: Flexible Commission Benchmark} \\ \cmidrule(l){2-5}&            &            &            \\" ) ///
    noobs nonumber ///
	fragment ///
	append /// 
	coeflabels(1 "Bust" 2 "Medium" 3 "Boom") ///
	collabels(,none) ///
	mlabels("",none) booktabs
	
	estpost tabstat sellp_wtBaseline_flexcBF buyp_wtBaseline_flexcBF sellV_wtBaseline_flexcBF buyV_wtBaseline_flexcBF, statistics(mean) 
foreach i in sellp_wtBaseline_flexcBF buyp_wtBaseline_flexcBF sellV_wtBaseline_flexcBF buyV_wtBaseline_flexcBF {
    matrix `i'n  = e(`i')
    matrix colnames `i'n = Total 
    estadd matrix  `i'n 
}
esttab using output//counterfactual_results_allBF.tex,  ///
    cells("sellp_wtBaseline_flexcBFn(fmt(%12.3fc)) buyp_wtBaseline_flexcBFn(fmt(%12.3fc)) sellV_wtBaseline_flexcBFn(fmt(%12.0fc)) buyV_wtBaseline_flexcBFn(fmt(%12.0fc))") ///
	fragment ///
	append /// 
	prefoot("\bottomrule") ///
    mlabels(,none) collabels(,none) ///
    noobs nonumber noline nomtitles ///
	booktabs
	
	*** Fixed Commission to match sale average committion of the Efficiency Benchmark
	estpost tabstat sellp_stCom_BFEffAvg buyp_stCom_BFEffAvg sellVCom_BFEffAvg buyVCom_BFEffAvg, statistics(mean) by(aggr_state) notot
esttab using output//counterfactual_results_allBF.tex,  ///
    cells("sellp_stCom_BFEffAvg(fmt(%12.3fc)) buyp_stCom_BFEffAvg(fmt(%12.3fc)) sellVCom_BFEffAvg(fmt(%12.0fc)) buyVCom_BFEffAvg(fmt(%12.0fc))") ///
	posthead("\hline \multicolumn{5}{c}{Panel C: Fixed Commission 1.61\%} \\ \cmidrule(l){2-5}&            &            &            \\" ) ///
    noobs nonumber ///
	fragment ///
	append /// 
	coeflabels(1 "Bust" 2 "Medium" 3 "Boom") ///
	collabels(,none) ///
	mlabels("",none) booktabs
	
	estpost tabstat sellp_wtCom_BFEffAvg buyp_wtCom_BFEffAvg sellV_wtCom_BFEffAvg buyV_wtCom_BFEffAvg, statistics(mean) 
foreach i in sellp_wtCom_BFEffAvg buyp_wtCom_BFEffAvg sellV_wtCom_BFEffAvg buyV_wtCom_BFEffAvg {
    matrix `i'n  = e(`i')
    matrix colnames `i'n = Total 
    estadd matrix  `i'n 
}
esttab using output//counterfactual_results_allBF.tex,  ///
    cells("sellp_wtCom_BFEffAvg(fmt(%12.3fc)) buyp_wtCom_BFEffAvg(fmt(%12.3fc)) sellV_wtCom_BFEffAvg(fmt(%12.0fc)) buyV_wtCom_BFEffAvg(fmt(%12.0fc))") ///
	fragment ///
	append /// 
	prefoot("\bottomrule") ///
    mlabels(,none) collabels(,none) ///
    noobs nonumber noline nomtitles ///
	booktabs
	
*** Entry Cost  - not fixing buyers
	estpost tabstat sellp_stEntryCostBF buyp_stEntryCostBF sellVEntryCostBF buyVEntryCostBF, statistics(mean) by(aggr_state) notot
esttab using output//counterfactual_results_allBF.tex,  ///
    cells("sellp_stEntryCostBF(fmt(%12.3fc)) buyp_stEntryCostBF(fmt(%12.3fc)) sellVEntryCostBF(fmt(%12.0fc)) buyVEntryCostBF(fmt(%12.0fc))") ///
	posthead("\hline \multicolumn{5}{c}{Panel C: Increased Entry Cost} \\ \cmidrule(l){2-5}&            &            &            \\" ) ///
    noobs nonumber ///
	fragment ///
	append /// 
	coeflabels(1 "Bust" 2 "Medium" 3 "Boom") ///
	collabels(,none) ///
	mlabels("",none) booktabs
	
	
estpost tabstat sellp_wtEntryCostBF buyp_wtEntryCostBF sellV_wtEntryCostBF buyV_wtEntryCostBF, statistics(mean) 
	foreach i in sellp_wtEntryCostBF buyp_wtEntryCostBF sellV_wtEntryCostBF buyV_wtEntryCostBF {
    matrix `i'n  = e(`i')
    matrix colnames `i'n = Total 
    estadd matrix  `i'n 
}
esttab using output//counterfactual_results_allBF.tex,  ///
    cells("sellp_wtEntryCostBFn(fmt(%12.3fc)) buyp_wtEntryCostBFn(fmt(%12.3fc)) sellV_wtEntryCostBFn(fmt(%12.0fc)) buyV_wtEntryCostBFn(fmt(%12.0fc))") ///
 	fragment ///
	append /// 
	prefoot("\bottomrule") ///
	postfoot("\end{tabular}") ///
    mlabels(,none) collabels(,none) ///
    noobs nonumber noline nomtitles ///
	booktabs

